From 29004a2137433728ef1f8c26e9329b94ecd8d605 Mon Sep 17 00:00:00 2001 From: robertl Date: Wed, 18 Sep 2002 16:37:48 +0000 Subject: [PATCH] Unify out of memory libc handling. Update dependencies. --- gpsbabel/Makefile | 20 +++++++++++++------- gpsbabel/cetus.c | 17 ++++++----------- gpsbabel/csv.c | 9 ++------- gpsbabel/csv_util.c | 6 +----- gpsbabel/defs.h | 6 ++++-- gpsbabel/garmin.c | 5 ++--- gpsbabel/geo.c | 8 ++++---- gpsbabel/gpsman.c | 10 +++------- gpsbabel/gpspilot.c | 11 ++++------- gpsbabel/gpsutil.c | 11 ++++------- gpsbabel/gpx.c | 7 ++----- gpsbabel/holux.c | 20 ++++++-------------- gpsbabel/magnav.c | 12 ++++-------- gpsbabel/magproto.c | 12 ++++-------- gpsbabel/mapsend.c | 6 +++--- gpsbabel/mapsource.c | 9 +++------ gpsbabel/mkshort.c | 33 ++++++++------------------------- gpsbabel/mxf.c | 16 +++------------- gpsbabel/ozi.c | 18 +++--------------- gpsbabel/pcx.c | 8 ++++---- gpsbabel/psp.c | 10 +++------- gpsbabel/tiger.c | 11 ++++------- gpsbabel/util.c | 37 +++++++++++++++++++++++++++++++++++++ 23 files changed, 127 insertions(+), 175 deletions(-) diff --git a/gpsbabel/Makefile b/gpsbabel/Makefile index a81e2e9d6..4fb6dfbf2 100644 --- a/gpsbabel/Makefile +++ b/gpsbabel/Makefile @@ -17,32 +17,38 @@ OBJS=main.o queue.o route.o waypt.o util.o vecs.o mkshort.o csv_util.o \ all: gpsbabel gpsbabel: $(OBJS) - $(CC) $(CFLAGS) $(OBJS) -o gpsbabel -lexpat -lm #-lpdb + $(CC) $(CFLAGS) $(OBJS) -o gpsbabel -lexpat -lm clean: rm -f $(OBJS) gpsbabel gpsbabel.exe cetus.o: cetus.c defs.h queue.h coldsync/palm.h coldsync/pdb.h -csv.o: csv.c defs.h queue.h +csv.o: csv.c defs.h queue.h csv_util.h +csv_util.o: csv_util.c defs.h queue.h csv_util.h +garmin.o: garmin.c defs.h queue.h jeeps/gps.h jeeps/gpsport.h \ + jeeps/gpsserial.h jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h \ + jeeps/gpsapp.h jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h \ + jeeps/gpsmath.h jeeps/gpsnmea.h jeeps/gpsmem.h jeeps/gpsrqst.h \ + jeeps/gpsinput.h jeeps/gpsproj.h jeeps/gpsnmeafmt.h jeeps/gpsnmeaget.h geo.o: geo.c defs.h queue.h gpsman.o: gpsman.c defs.h queue.h gpspilot.o: gpspilot.c defs.h queue.h coldsync/palm.h coldsync/pdb.h gpsutil.o: gpsutil.c defs.h queue.h magellan.h gpx.o: gpx.c defs.h queue.h +holux.o: holux.c defs.h queue.h holux.h magnav.o: magnav.c defs.h queue.h coldsync/palm.h coldsync/pdb.h magproto.o: magproto.c defs.h queue.h magellan.h main.o: main.c defs.h queue.h mapsend.o: mapsend.c defs.h queue.h mapsend.h mapsource.o: mapsource.c defs.h queue.h -mkshort.o: mkshort.c +mkshort.o: mkshort.c defs.h queue.h +mxf.o: mxf.c defs.h queue.h csv_util.h +ozi.o: ozi.c defs.h queue.h csv_util.h pcx.o: pcx.c defs.h queue.h +psp.o: psp.c defs.h queue.h queue.o: queue.c queue.h route.o: route.c defs.h queue.h tiger.o: tiger.c defs.h queue.h magellan.h util.o: util.c defs.h queue.h vecs.o: vecs.c defs.h queue.h waypt.o: waypt.c defs.h queue.h -psp.o: psp.c defs.h queue.h -mxf.o: mxf.c csv_util.c defs.h queue.h csv_util.h -ozi.o: ozi.c csv_util.c defs.h queue.h csv_util.h -csv_util.o: csv_util.c csv_util.h defs.h diff --git a/gpsbabel/cetus.c b/gpsbabel/cetus.c index 8443adaa4..c834ab67b 100644 --- a/gpsbabel/cetus.c +++ b/gpsbabel/cetus.c @@ -108,14 +108,11 @@ data_read(void) for(pdb_rec = pdb->rec_index.rec; pdb_rec; pdb_rec=pdb_rec->next) { waypoint *wpt_tmp; - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); rec = (struct record *) pdb_rec->data; - wpt_tmp->shortname = strdup(rec->ID); - wpt_tmp->description = strdup(rec->name); + wpt_tmp->shortname = xstrdup(rec->ID); + wpt_tmp->description = xstrdup(rec->name); wpt_tmp->position.altitude.altitude_meters = pdb_read4(&rec->elevation) / 100.0; wpt_tmp->position.longitude.degrees = pdb_read4(&rec->longitude) / 10000000.0; @@ -146,7 +143,7 @@ cetus_writewpt(waypoint *wpt) static int ct; struct tm *tm; - rec = calloc(sizeof(*rec),1); + rec = xcalloc(sizeof(*rec),1); strncpy(rec->ID, wpt->shortname, sizeof(rec->ID)); rec->ID[sizeof(rec->ID)-1] = 0; @@ -226,11 +223,9 @@ data_write(void) * Turns out plain old strcmp will do the trick... */ - htable = malloc(ct * sizeof(*htable)); + htable = xmalloc(ct * sizeof(*htable)); bh = htable; - if (!htable) { - fatal (MYNAME ":Cannot get array for sorting waypoints."); - } + QUEUE_FOR_EACH(&waypt_head, elem, tmp) { waypointp = (waypoint *) elem; bh->wpt = waypointp; diff --git a/gpsbabel/csv.c b/gpsbabel/csv.c index a7ba546a8..8358b698f 100644 --- a/gpsbabel/csv.c +++ b/gpsbabel/csv.c @@ -75,10 +75,7 @@ data_read(void) if (strlen(buff)) { - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); s = buff; /* data delimited by commas, not enclosed */ @@ -95,9 +92,7 @@ data_read(void) wpt_tmp->position.longitude.degrees = atof(s); break; case 2: - wpt_tmp->description = strdup(s); - if (! wpt_tmp->description) - fatal(MYNAME, ": cannot allocate memory\n"); + wpt_tmp->description = xstrdup(s); wpt_tmp->description = csv_stringtrim(wpt_tmp->description, " "); break; default: diff --git a/gpsbabel/csv_util.c b/gpsbabel/csv_util.c index 4471b7bd1..35e18bd5e 100644 --- a/gpsbabel/csv_util.c +++ b/gpsbabel/csv_util.c @@ -180,11 +180,7 @@ csv_lineparse(char *stringstart, const char *delimited_by, } /* allocate enough space for this data field */ - tmp = (char *) calloc((p - sp) + 1, sizeof(char)); - - if (!tmp) { - fatal(MYNAME ": cannot allocate memory\n"); - } + tmp = xcalloc((p - sp) + 1, sizeof(char)); strncpy(tmp, sp, (p - sp)); diff --git a/gpsbabel/defs.h b/gpsbabel/defs.h index 9ca580c85..f8d892fdb 100644 --- a/gpsbabel/defs.h +++ b/gpsbabel/defs.h @@ -112,10 +112,12 @@ void waypt_disp(waypoint *); void fatal(const char *, ...); ff_vecs_t *find_vec(char *); void disp_vecs(void); - - void printposn(coord *c, int is_lat); +void *xcalloc(size_t nmemb, size_t size); +void *xmalloc(size_t size); +char * xstrdup(const char *s); + /* * Data types for Palm/OS files. */ diff --git a/gpsbabel/garmin.c b/gpsbabel/garmin.c index 5b586b4f9..7d2530988 100644 --- a/gpsbabel/garmin.c +++ b/gpsbabel/garmin.c @@ -54,7 +54,7 @@ data_read(void) } for (i = 0; i < n; i++) { - waypoint *wpt_tmp = calloc(sizeof(*wpt_tmp),1); + waypoint *wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); wpt_tmp->shortname = way[i]->ident; wpt_tmp->description = way[i]->cmnt; @@ -77,9 +77,8 @@ data_write(void) extern queue waypt_head; GPS_PWay *way; + way = xmalloc(n*sizeof(*way)); - if(!(way=(GPS_PWay *)malloc(n*sizeof(GPS_PWay *)))) - fatal(MYNAME ":not enough memory\n"); for (i = 0; i < n; i++) { if(!((way)[i]=GPS_Way_New())) fatal(MYNAME ":not enough memory\n"); diff --git a/gpsbabel/geo.c b/gpsbabel/geo.c index 09a0c8d7e..1333c20f8 100644 --- a/gpsbabel/geo.c +++ b/gpsbabel/geo.c @@ -56,7 +56,7 @@ tag_name(const char **attrv) const char **avp = &attrv[0]; while (*avp) { if (strcmp(avp[0], "id") == 0) { - wpt_tmp->shortname = strdup(avp[1]); + wpt_tmp->shortname = xstrdup(avp[1]); } avp+=2; } @@ -68,7 +68,7 @@ tag_link(const char **attrv) const char **avp = &attrv[0]; while (*avp) { if (strcmp(avp[0], "text") == 0) { - wpt_tmp->url_link_text = strdup(avp[1]); + wpt_tmp->url_link_text = xstrdup(avp[1]); } avp+=2; } @@ -91,7 +91,7 @@ geo_start(void *data, const char *el, const char **attr) } if (strcmp(el, "waypoint") == 0) { - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); in_wpt++; } else if (strcmp(el, "name") == 0) { in_name++; @@ -119,7 +119,7 @@ geo_end(void *data, const char *el) static void geo_cdata(void *dta, const XML_Char *s, int len) { - char *foo = malloc(len+1); + char *foo = xmalloc(len+1); foo[len] = 0; strncpy(foo, s, len); if (in_name) { diff --git a/gpsbabel/gpsman.c b/gpsbabel/gpsman.c index c67f0af6d..797fe4aa4 100644 --- a/gpsbabel/gpsman.c +++ b/gpsbabel/gpsman.c @@ -96,11 +96,7 @@ gpsman_read(void) sscanf(ibuf, "%[^\t] %[^\t] %c%d %lf %c%d %lf", sname, descr, &latdir, &latm, &latf, &londir, &lonm, &lonf); - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - - if (wpt_tmp == NULL) { - fatal(MYNAME ": Cannot allocate enough memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); lat = latm + latf; lon = lonm + lonf; @@ -110,8 +106,8 @@ gpsman_read(void) wpt_tmp->position.longitude.degrees = lon; wpt_tmp->position.latitude.degrees = lat; - wpt_tmp->shortname = strdup(sname); - wpt_tmp->description = strdup(descr); + wpt_tmp->shortname = xstrdup(sname); + wpt_tmp->description = xstrdup(descr); waypt_add(wpt_tmp); } diff --git a/gpsbabel/gpspilot.c b/gpsbabel/gpspilot.c index ed124d6f8..22e154165 100644 --- a/gpsbabel/gpspilot.c +++ b/gpsbabel/gpspilot.c @@ -90,10 +90,7 @@ data_read(void) waypoint *wpt_tmp; char *vdata; - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); rec = (struct record *) pdb_rec->data; wpt_tmp->position.longitude.degrees = pdb_read4(&rec->longitude) / 3.6e6; @@ -110,11 +107,11 @@ data_read(void) * receiver) we use that for shortname and use 'name' as * our description. */ - wpt_tmp->description = strdup(vdata); + wpt_tmp->description = xstrdup(vdata); vdata = vdata + strlen(vdata) + 1; vdata = vdata + strlen(vdata) + 1; - wpt_tmp->shortname = strdup(vdata); + wpt_tmp->shortname = xstrdup(vdata); waypt_add(wpt_tmp); @@ -130,7 +127,7 @@ cetus_writewpt(waypoint *wpt) static int ct; struct tm *tm; - rec = calloc(sizeof(*rec),1); + rec = xcalloc(sizeof(*rec),1); #if FIXME strncpy(rec->ID, wpt->shortname, sizeof(rec->ID)); rec->ID[sizeof(rec->ID)-1] = 0; diff --git a/gpsbabel/gpsutil.c b/gpsbabel/gpsutil.c index 550d83113..6e10c1d9b 100644 --- a/gpsbabel/gpsutil.c +++ b/gpsbabel/gpsutil.c @@ -55,13 +55,10 @@ data_read(void) while( fscanf(file_in, "%s %le%c %le%c %ld%c %30[^,] %c", name, &lat, &latdir, &lon, &londir, &alt, &alttype, desc, icon) > 0) { - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); wpt_tmp->position.altitude.altitude_meters = alt; - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(desc); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(desc); wpt_tmp->creation_time = time(NULL); if (latdir == 'S') lat = -lat; @@ -73,7 +70,7 @@ data_read(void) wpt_tmp->position.longitude.degrees = ilon + (lon - ilon)*(100.0/60.0); ilat = (int)(lat); wpt_tmp->position.latitude.degrees = ilat + (lat - ilat) * (100.0/60.0); - wpt_tmp->icon_descr = strdup(icon); + wpt_tmp->icon_descr = xstrdup(icon); waypt_add(wpt_tmp); } diff --git a/gpsbabel/gpx.c b/gpsbabel/gpx.c index 2c681ccaa..97c417725 100644 --- a/gpsbabel/gpx.c +++ b/gpsbabel/gpx.c @@ -60,10 +60,7 @@ tag_wpt(const char **attrv) { const char **avp = &attrv[0]; - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); while (*avp) { if (strcmp(avp[0], "lat") == 0) { @@ -125,7 +122,7 @@ gpx_end(void *data, const char *el) static void gpx_cdata(void *dta, const XML_Char *s, int len) { - char *foo = malloc(len+1); + char *foo = xmalloc(len+1); foo[len] = 0; strncpy(foo, s, len); if (in_name && in_wpt) { diff --git a/gpsbabel/holux.c b/gpsbabel/holux.c index 2fbf876e7..0bc0bd1e6 100644 --- a/gpsbabel/holux.c +++ b/gpsbabel/holux.c @@ -58,13 +58,9 @@ static void rd_deinit(void) static void wr_init(const char *fname) { - HxWFile = calloc(GM100_WPO_FILE_SIZE, 1); - if (HxWFile == NULL) - { - fatal("GPSBABEL: Cannot alloc memory\n"); - } + HxWFile = xcalloc(GM100_WPO_FILE_SIZE, 1); - strcpy (fOutname,fname); + strcpy (fOutname,fname); } @@ -95,11 +91,7 @@ static void data_read(void) struct tm *ptm; - HxWpt = calloc(GM100_WPO_FILE_SIZE, 1); - if (HxWpt == NULL) - { - fatal("GPSBABEL: Cannot alloc memory\n"); - } + HxWpt = xcalloc(GM100_WPO_FILE_SIZE, 1); /* read the wpo file to the data-array */ iDataRead = fread( HxWpt, 1, GM100_WPO_FILE_SIZE, file_in ); @@ -115,7 +107,7 @@ static void data_read(void) /* Get the waypoints */ for (iCount = 0; iCount < iWptNum ; iCount ++) { - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); iWptIndex = ((WPTHDR *)HxWpt)->idx[iCount]; /* get the waypoint index */ dwIndex= OFFS_WPT + (sizeof(WPT) * iWptIndex); @@ -128,8 +120,8 @@ static void data_read(void) strncpy(desc,pWptHxTmp->comment,sizeof(pWptHxTmp->comment)); desc[sizeof(pWptHxTmp->comment)]=0; - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(desc); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(desc); wpt_tmp->creation_time = 0; if (pWptHxTmp->date.year) diff --git a/gpsbabel/magnav.c b/gpsbabel/magnav.c index 39da81c99..699b27f76 100644 --- a/gpsbabel/magnav.c +++ b/gpsbabel/magnav.c @@ -105,11 +105,7 @@ data_read(void) char *vdata; struct tm tm = {0}; - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } - + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); rec = (struct record *) pdb_rec->data; wpt_tmp->position.altitude.altitude_meters = pdb_read4(&rec->elevation); @@ -118,10 +114,10 @@ data_read(void) vdata = (char *) pdb_rec->data + sizeof(*rec); - wpt_tmp->shortname = strdup(vdata); + wpt_tmp->shortname = xstrdup(vdata); vdata += strlen (vdata) + 1; - wpt_tmp->description = strdup(vdata); + wpt_tmp->description = xstrdup(vdata); vdata += strlen (vdata) + 1; tm.tm_sec = pdb_read2(&rec->crt_sec); @@ -146,7 +142,7 @@ my_writewpt(waypoint *wpt) static int ct; struct tm *tm; abort(); - rec = calloc(sizeof(*rec),1); + rec = xcalloc(sizeof(*rec),1); #if 0 strncpy(rec->ID, wpt->shortname, sizeof(rec->ID)); rec->ID[sizeof(rec->ID)-1] = 0; diff --git a/gpsbabel/magproto.c b/gpsbabel/magproto.c index fd1444afe..86f7d9211 100644 --- a/gpsbabel/magproto.c +++ b/gpsbabel/magproto.c @@ -562,9 +562,7 @@ mag_trkparse(char *trkmsg) struct tm tm; waypoint *waypt; - waypt = calloc(sizeof *waypt, 1); - if (waypt == NULL) - return NULL; + waypt = xcalloc(sizeof *waypt, 1); printf("%s\n", trkmsg); memset(&tm, 0, sizeof(tm)); @@ -678,9 +676,7 @@ mag_wptparse(char *trkmsg) descr[0] = 0; icon_token[0] = 0; - waypt = calloc(sizeof *waypt, 1); - if (waypt == NULL) - fatal(MYNAME ": Cannot allocate memory\n"); + waypt = xcalloc(sizeof *waypt, 1); sscanf(trkmsg,"$PMGNWPL,%lf,%c,%lf,%c,%d,%c,%[^,],%[^,]", &latdeg,&latdir, @@ -699,8 +695,8 @@ mag_wptparse(char *trkmsg) waypt->position.longitude.degrees = mag2degrees(lngdeg); waypt->position.altitude.altitude_meters = alt; - waypt->shortname = strdup(shortname); - waypt->description = strdup(descr); + waypt->shortname = xstrdup(shortname); + waypt->description = xstrdup(descr); waypt->icon_descr = mag_find_descr_from_token(icon_token); return waypt; diff --git a/gpsbabel/mapsend.c b/gpsbabel/mapsend.c index 0e4dd6e96..7d9d3ac41 100644 --- a/gpsbabel/mapsend.c +++ b/gpsbabel/mapsend.c @@ -206,7 +206,7 @@ mapsend_read(void) my_fread4(&wpt_count, mapsend_file_in); while (wpt_count--) { - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); fread(&scount, sizeof(scount), 1, mapsend_file_in); fread(tbuf, scount, 1, mapsend_file_in); @@ -225,8 +225,8 @@ mapsend_read(void) my_fread8(&wpt_long, mapsend_file_in); my_fread8(&wpt_lat, mapsend_file_in); - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(comment); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(comment); wpt_tmp->position.altitude.altitude_meters = wpt_alt; wpt_tmp->position.latitude.degrees = -wpt_lat; wpt_tmp->position.longitude.degrees = wpt_long; diff --git a/gpsbabel/mapsource.c b/gpsbabel/mapsource.c index da715d287..fe675a596 100644 --- a/gpsbabel/mapsource.c +++ b/gpsbabel/mapsource.c @@ -78,14 +78,11 @@ mapsource_read(void) sscanf(ibuf, "Waypoint %s %s %s %c%d %f %c%d %f %d %s Symbol & Name %s", name, date, timeb, &latdir, &latd, &latf, &londir, &lond, &lonf, &alt, altunits, icon); - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal("MAPSOURCE: cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); /* FIXME: Implement actual appropriate conversion */ wpt_tmp->position.altitude.altitude_meters = alt * 3.0; - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(name); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(name); lat = latd + latf/100.0; lon = lond + lonf/100.0; diff --git a/gpsbabel/mkshort.c b/gpsbabel/mkshort.c index 4ca5c499e..4fde0e11d 100644 --- a/gpsbabel/mkshort.c +++ b/gpsbabel/mkshort.c @@ -32,7 +32,7 @@ delete_last_vowel(int start, char *istring, int *replaced) */ for (l = strlen(istring); l > start; l--) { if (strchr(vowels, istring[l-1])) { - char *ostring = strdup(istring); + char *ostring = xstrdup(istring); strncpy(&ostring[l-1], &istring[l], 1+strlen(istring)-l); ostring[strlen(istring)-1] = 0; @@ -76,7 +76,7 @@ setshort_badchars(const char *s) if (s == NULL) { badchars = DEFAULT_BADCHARS; } else { - badchars = strdup(s); + badchars = xstrdup(s); } } @@ -89,28 +89,20 @@ setshort_mustupper(int i) char * mkshort(const char *istring) { - char *ostring = strdup(istring); + char *ostring = xstrdup(istring); char *nstring; char *tstring; char *cp; char *np; int i, l, nlen, replaced; - - if (!ostring) { - fatal("mkshort: could not reallocate memory for string\n"); - } - /* * Whack leading "[Tt]he", */ if (( strlen(ostring) > target_len + 4) && (strncmp(ostring, "The ", 4) == 0 || strncmp(ostring, "the ", 4) == 0)) { - nstring = strdup(ostring + 4); - if (!nstring) { - fatal(needmem); - } + nstring = xstrdup(ostring + 4); free(ostring); ostring = nstring; } @@ -119,7 +111,7 @@ mkshort(const char *istring) * Look at the back of the string for " by BLAH" and whack * it there. */ - nstring = strdup(ostring); + nstring = xstrdup(ostring); l = strlen (nstring); while (l > 0) { if (strncmp(&nstring[l], " by ",4) == 0) { @@ -135,10 +127,7 @@ mkshort(const char *istring) /* * Eliminate Whitespace */ - tstring = strdup(ostring); - if (!tstring) { - abort(); - } + tstring = xstrdup(ostring); l = strlen (tstring); cp = ostring; for (i=0;iposition.longitude.degrees = atof(s); break; case 2: - wpt_tmp->description = strdup(s); - if (! wpt_tmp->description) - fatal(MYNAME, ": cannot allocate memory\n"); - + wpt_tmp->description = xstrdup(s); wpt_tmp->description = csv_stringtrim(wpt_tmp->description, ""); break; case 3: - wpt_tmp->shortname = strdup(s); - if (! wpt_tmp->shortname) - fatal(MYNAME, ": cannot allocate memory\n"); - + wpt_tmp->shortname = xstrdup(s); csv_stringtrim(wpt_tmp->shortname, ""); break; case 4: diff --git a/gpsbabel/ozi.c b/gpsbabel/ozi.c index 79841f6a0..91f0bd2e4 100644 --- a/gpsbabel/ozi.c +++ b/gpsbabel/ozi.c @@ -81,11 +81,7 @@ data_read(void) if ((strlen(buff)) && (strstr(buff, ",") != NULL)) { - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); - - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); /* data delimited by commas, possibly enclosed in quotes. */ s = buff; @@ -99,11 +95,7 @@ data_read(void) break; case 1: /* waypoint name */ - wpt_tmp->shortname = strdup(s); - - if (! wpt_tmp->shortname) - fatal(MYNAME, ": cannot allocate memory\n"); - + wpt_tmp->shortname = xstrdup(s); csv_stringtrim(wpt_tmp->shortname, ""); break; case 2: @@ -135,11 +127,7 @@ data_read(void) break; case 10: /* Description */ - wpt_tmp->description = strdup(s); - - if (! wpt_tmp->description) - fatal(MYNAME, ": cannot allocate memory\n"); - + wpt_tmp->description = xstrdup(s); wpt_tmp->description = csv_stringtrim(wpt_tmp->description, ""); break; diff --git a/gpsbabel/pcx.c b/gpsbabel/pcx.c index dfd45aa0a..9b5b347c5 100644 --- a/gpsbabel/pcx.c +++ b/gpsbabel/pcx.c @@ -78,16 +78,16 @@ data_read(void) sscanf(ibuf, "W %s %c%lf %c%lf %s %s %ld %90[^\n']", name, &latdir, &lat, &londir, &lon, date, time, &alt, desc); - wpt_tmp = calloc(sizeof(*wpt_tmp), 1); + wpt_tmp = xcalloc(sizeof(*wpt_tmp), 1); wpt_tmp->position.altitude.altitude_meters = alt; - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(desc); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(desc); if (latdir == 'S') lat = -lat; if (londir == 'W') lon = -lon; wpt_tmp->position.longitude.degrees = lon/100.0; wpt_tmp->position.latitude.degrees = lat/100.0; - wpt_tmp->icon_descr = strdup(icon); + wpt_tmp->icon_descr = xstrdup(icon); waypt_add(wpt_tmp); } } diff --git a/gpsbabel/psp.c b/gpsbabel/psp.c index 98baa83ce..826ec6418 100644 --- a/gpsbabel/psp.c +++ b/gpsbabel/psp.c @@ -135,11 +135,7 @@ psp_read(void) pincount = *(short int *)&buff[12]; while (pincount--) { - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); /* things we will probably never know about this waypoint */ /* coming from a pushpin file. */ @@ -198,7 +194,7 @@ psp_read(void) buffer_washer(buff, stringsize); - wpt_tmp->shortname = strdup(buff); + wpt_tmp->shortname = xstrdup(buff); /* 1 bytes string size */ psp_fread(&buff[0], 1, 1, psp_file_in); @@ -215,7 +211,7 @@ psp_read(void) buffer_washer(buff, stringsize); - wpt_tmp->description = strdup(buff); + wpt_tmp->description = xstrdup(buff); /* 1 bytes - string size */ psp_fread(&buff[0], 1, 1, psp_file_in); diff --git a/gpsbabel/tiger.c b/gpsbabel/tiger.c index 5978153eb..34a5196c1 100644 --- a/gpsbabel/tiger.c +++ b/gpsbabel/tiger.c @@ -73,20 +73,17 @@ abort(); while( fscanf(file_in, "%s %le%c %le%c %ld%c %30[^,] %c", name, &lat, &latdir, &lon, &londir, &alt, &alttype, desc, icon) > 0) { - wpt_tmp = calloc(sizeof(*wpt_tmp),1); - if (wpt_tmp == NULL) { - fatal(MYNAME ": cannot allocate memory\n"); - } + wpt_tmp = xcalloc(sizeof(*wpt_tmp),1); wpt_tmp->position.altitude.altitude_meters = alt; - wpt_tmp->shortname = strdup(name); - wpt_tmp->description = strdup(desc); + wpt_tmp->shortname = xstrdup(name); + wpt_tmp->description = xstrdup(desc); wpt_tmp->creation_time = time(NULL); if (latdir == 'S') lat = -lat; if (londir == 'W') lon = -lon; wpt_tmp->position.longitude.degrees = lon/100.0; wpt_tmp->position.latitude.degrees = lat/100.0; - wpt_tmp->icon_descr = strdup(icon); + wpt_tmp->icon_descr = xstrdup(icon); waypt_add(wpt_tmp); } diff --git a/gpsbabel/util.c b/gpsbabel/util.c index e70bf7f54..68504ae22 100644 --- a/gpsbabel/util.c +++ b/gpsbabel/util.c @@ -23,6 +23,43 @@ #include #include +void * +xmalloc(size_t size) +{ + void *obj = malloc(size); + + if (!obj) { + fatal("gpsbabel: Unable to allocate %d bytes of memory.\n"); + } + + return obj; +} + +void * +xcalloc(size_t nmemb, size_t size) +{ + void *obj = calloc(nmemb, size); + + if (!obj) { + fatal("gpsbabel: Unable to allocate %d bytes of memory.\n"); + } + + return obj; +} + +char * +xstrdup(const char *s) +{ + char *o = strdup(s); + + if (!o) { + fatal("gpsbabel: Unable to allocate %d bytes of memory.\n"); + } + + return o; +} + + coord mkposn(const char *string) { -- 2.30.2